package com.weather.domen.impl;

import com.weather.domen.DeviceDomeService;
import com.weather.pojo.entity.DeviceInfo;
import com.weather.pojo.vo.DeviceRuntimeDataVO;
import com.weather.service.IDeviceInfoService;
import com.weather.service.IDeviceRuntimeDataService;
import com.weather.service.IAreaInfoService;
import com.weather.pojo.vo.DeviceInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;
import com.weather.pojo.entity.AreaInfo;
import com.weather.pojo.entity.DeviceRuntimeData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weather.pojo.entity.AreaDeviceRelation;
import com.weather.service.IAreaDeviceRelationService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DeviceDomeServiceImpl implements DeviceDomeService {
    @Resource
    private IDeviceInfoService deviceInfoService;

    @Resource
    private IDeviceRuntimeDataService  deviceRuntimeDataService;

    @Resource
    private IAreaInfoService areaInfoService;

    @Resource
    private IAreaDeviceRelationService areaDeviceRelationService;

    @Override
    public List<DeviceInfoVO> getDeviceList() {
        List<DeviceInfo> deviceList = deviceInfoService.list();
        List<DeviceInfoVO> deviceVOList = new ArrayList<>();

        for (DeviceInfo deviceInfo : deviceList) {
            DeviceInfoVO deviceVO = new DeviceInfoVO();
            deviceVO.setDeviceId(deviceInfo.getDeviceId());
            deviceVO.setDeviceName(deviceInfo.getDeviceName());
            deviceVO.setDeviceType(deviceInfo.getDeviceType());
            deviceVO.setModelNumber(deviceInfo.getModelNumber());
            deviceVO.setProductionDate(deviceInfo.getProductionDate());
            deviceVO.setPurchaseDate(deviceInfo.getPurchaseDate());
            deviceVO.setWarrantyPeriod(deviceInfo.getWarrantyPeriod());
            deviceVO.setRatedPower(deviceInfo.getRatedPower());
            deviceVO.setBladeLength(deviceInfo.getBladeLength());
            deviceVO.setTowerHeight(deviceInfo.getTowerHeight());
            deviceVO.setStatus(deviceInfo.getStatus());
            deviceVO.setInstallTime(deviceInfo.getInstallTime());

            if (deviceInfo.getCurrentAreaId() != null) {
                AreaInfo areaInfo = areaInfoService.getById(deviceInfo.getCurrentAreaId());
                if (areaInfo != null) {
                    deviceVO.setAreaName(areaInfo.getAreaName());
                }
            }

            deviceVOList.add(deviceVO);
        }

        return deviceVOList;
    }

    @Override
    public DeviceRuntimeDataVO getDeviceRuntimeDataById(Long deviceId) {
        LambdaQueryWrapper<DeviceRuntimeData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceRuntimeData::getDeviceId, deviceId)
                    .orderByDesc(DeviceRuntimeData::getRecordTime)
                    .last("limit 1");

        DeviceRuntimeData runtimeData = deviceRuntimeDataService.getOne(queryWrapper);

        if (runtimeData == null) {
            return null;
        }

        DeviceInfo deviceInfo = deviceInfoService.getById(deviceId);

        DeviceRuntimeDataVO runtimeDataVO = new DeviceRuntimeDataVO();

        runtimeDataVO.setRecordTime(runtimeData.getRecordTime());
        runtimeDataVO.setPowerOutput(runtimeData.getPowerOutput());
        runtimeDataVO.setWindSpeed(runtimeData.getWindSpeed());
        runtimeDataVO.setRotorSpeed(runtimeData.getRotorSpeed());
        runtimeDataVO.setTemperature(runtimeData.getTemperature());
        runtimeDataVO.setHumidity(runtimeData.getHumidity());
        runtimeDataVO.setVibrationLevel(runtimeData.getVibrationLevel());
        runtimeDataVO.setOperationHours(runtimeData.getOperationHours());
        runtimeDataVO.setStatusCode(runtimeData.getStatusCode());

        if (deviceInfo != null) {
            runtimeDataVO.setDeviceName(deviceInfo.getDeviceName());
            runtimeDataVO.setDeviceType(deviceInfo.getDeviceType());
            runtimeDataVO.setModelNumber(deviceInfo.getModelNumber());
            runtimeDataVO.setRatedPower(deviceInfo.getRatedPower());

            if (deviceInfo.getCurrentAreaId() != null) {
                AreaInfo areaInfo = areaInfoService.getById(deviceInfo.getCurrentAreaId());
                if (areaInfo != null) {
                    runtimeDataVO.setAreaName(areaInfo.getAreaName());
                }
            }
        }
        return runtimeDataVO;
    }

    @Override
    @Transactional
    public boolean addDevice(DeviceInfo deviceInfo) {
        deviceInfoService.save(deviceInfo);
        AreaDeviceRelation areaDeviceRelation = new AreaDeviceRelation();
        areaDeviceRelation.setDeviceId(deviceInfo.getDeviceId());
        areaDeviceRelation.setAreaId(deviceInfo.getCurrentAreaId());
        areaDeviceRelationService.save(areaDeviceRelation);
        return true;
    }

    @Override
    public boolean updateDevice(DeviceInfo deviceInfo) {
        return deviceInfoService.updateById(deviceInfo);
    }

    @Override
    @Transactional
    public boolean deleteDevice(Long deviceId) {
        LambdaQueryWrapper<AreaDeviceRelation> areaRelationQueryWrapper = new LambdaQueryWrapper<>();
        areaRelationQueryWrapper.eq(AreaDeviceRelation::getDeviceId, deviceId);
        areaDeviceRelationService.remove(areaRelationQueryWrapper);

        LambdaQueryWrapper<DeviceRuntimeData> runtimeDataQueryWrapper = new LambdaQueryWrapper<>();
        runtimeDataQueryWrapper.eq(DeviceRuntimeData::getDeviceId, deviceId);
        deviceRuntimeDataService.remove(runtimeDataQueryWrapper);

        return deviceInfoService.removeById(deviceId);
    }
}
